﻿package org.display.util
{
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.utils.Timer;
	
	import org.data.*;
	import org.utils.*;

	public class ExecuteAllJsfl
	{
		var jsfls:Array=[]
			
		var publicfiles:Array=[]
			
		public function ExecuteAllJsfl(path:String)
		{

			openFolder(path)
				
			jsfls=getJsflsPath()
				
			//trace(publicfiles+"/////")
			
			for(var k=0;k<jsfls.length;k++){
			   
				var del:Boolean=true
					
			    for(var p=0;p<publicfiles.length;p++){
				
					if(jsfls[k].url==publicfiles[p].url){
                        
						del=false
					}		
				}
				
				if(del){
				jsfls.splice(k,1)
				k--
				}

			}
			
			for( k=0;k<jsfls.length;k++){
			    trace(jsfls[k].url)
			}
			
			var timer:Timer=new Timer(1000)
			timer.addEventListener(TimerEvent.TIMER,timerEvent)
			timer.start()

		}
		
		
		
		function timerEvent(e:TimerEvent){
			
			try{
				
				var file:File =jsfls.shift()
				file.openWithDefaultApplication();
				
			}catch(e:Error){trace(e)}
			
			    if(jsfls.length==0){
				
				e.currentTarget.removeEventListener(TimerEvent.TIMER,timerEvent)
					
				e.currentTarget.stop();
				
			    }
			
		}
		
	   function getJsflsPath():Array{
			
			var ivs:Array=Global.jsflsPath.concat()
			
			for(var j=0;j<ivs.length;j++){
				
				ivs[j].iv=ivs[j].ivc.getIvs().join("_")
					
			}
			
			var jsfls:Array=[]
				
			ivs=ArrayControl.orderRoot(ivs)
				
			for(var i=0;i<ivs.length;i++){
				
				jsfls.push(File.documentsDirectory.resolvePath(ivs[i].path))
				//trace(ivs[i].path+":"+ivs[i].ivname)
				
			}
			
			return jsfls
			
		}
		
		
		function openFolder(path:String){

			var file:File=File.desktopDirectory.resolvePath(path)
			var files:Array=file.getDirectoryListing() 
				
			for(var i=0;i<files.length;i++){
				
				if(files[i].isDirectory){
					openFolder(files[i].url)
				}else{
					publicfiles.push(files[i])
				}
			}

		}
		
	}
}